package gao.xiaolei.controller;

import java.io.IOException;
import javax.annotation.Resource;

import gao.xiaolei.service.ImageService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@CrossOrigin
public class ImageUploadController {

    @Resource(name = "ImageService")
    private ImageService imageService;

    @PostMapping("/")
    public String handleFileUpload(@RequestParam(name = "file", required = true) MultipartFile file,
    		@RequestParam(name = "defineHashCode", required = false) String defineHashCode) throws IOException {
		return imageService.uploadImage(file,defineHashCode);
    }

	@DeleteMapping(value = "/{hashCode}")
	public String handleFileDelete(@PathVariable("hashCode") String hashCode) {
		return imageService.deleteImage(hashCode);
    }

	@GetMapping("/{hashCode}")
	public byte[] handleFileGet(@PathVariable("hashCode") String hashCode) throws IOException {
    	return imageService.getImage(hashCode);
    }
}
